package com.xpsoft.oa.action.produce;


import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;
import com.xpsoft.core.command.QueryFilter;
import com.xpsoft.core.util.JsonUtil;
import com.xpsoft.core.web.action.BaseAction;
import com.xpsoft.oa.model.produce.ArticleReceive;
import com.xpsoft.oa.model.produce.ArticleReg;
import com.xpsoft.oa.model.produce.ArticleSend;
import com.xpsoft.oa.service.produce.ArticleRegService;

import flexjson.JSONSerializer;
/**
 * 
 * @author 
 *
 */
public class ArticleRegAction extends BaseAction{
	@Resource
	private ArticleRegService articleRegService;
	private ArticleReg articleReg;
	private ArticleReceive articleReceive;
	private ArticleSend articleSend;
	
	private Long id;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public ArticleReg getArticleReg() {
		return articleReg;
	}

	public void setArticleReg(ArticleReg articleReg) {
		this.articleReg = articleReg;
	}
	

	public ArticleReceive getArticleReceive() {
		return articleReceive;
	}

	public void setArticleReceive(ArticleReceive articleReceive) {
		this.articleReceive = articleReceive;
	}

	public ArticleSend getArticleSend() {
		return articleSend;
	}

	public void setArticleSend(ArticleSend articleSend) {
		this.articleSend = articleSend;
	}

	/**
	 * 显示列表
	 */
	public String list(){
		
		QueryFilter filter=new QueryFilter(getRequest());
		List<ArticleReg> list= articleRegService.getAll(filter);
		
		StringBuffer buff = new StringBuffer("{success:true,'totalCounts':")
		.append(filter.getPagingBean().getTotalItems()).append(",result:");
		JSONSerializer jsonSerializer = new JSONSerializer();
		buff.append(jsonSerializer.exclude(new String[] { "class" }).include("project","managerUser").serialize(list));
	    buff.append("}");
		
		jsonString=buff.toString();
		
		return SUCCESS;
	}
	/**
	 * 批量删除
	 * @return
	 */
	public String multiDel(){
		
		String[]ids=getRequest().getParameterValues("ids");
		if(ids!=null){
			for(String id:ids){
				articleRegService.remove(new Long(id));
			}
		}
		
		jsonString="{success:true}";
		
		return SUCCESS;
	}
	
	/**
	 * 显示详细信息
	 * @return
	 */
	public String get(){
		ArticleReg articleReg=articleRegService.get(id);
		
		//将数据转成JSON格式
		StringBuffer sb = new StringBuffer("{success:true,data:");
		JSONSerializer jsonSerializer = new JSONSerializer();
		sb.append(jsonSerializer.exclude(new String[] { "class" }).include("project","managerUser").serialize(articleReg));
		sb.append("}");
		setJsonString(sb.toString());
		
		return SUCCESS;
	}
	/**
	 * 添加及保存操作
	 */
	public String save(){
		articleRegService.saveArticleReg(articleReg, articleReceive, articleSend);
		setJsonString("{success:true}");
		return SUCCESS;
	}
}
